package com.github.cdisk.xtrans.ui.widget;

import java.text.SimpleDateFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.github.cdisk.xtrans.core.ConfigFile;


public class TimeIntervalDialog extends Dialog {

	private static final SimpleDateFormat HHMMSS_SDF = new SimpleDateFormat("HHmmss");

	private ConfigFile config;
	protected Shell shell;
	private Button button;
	private Button btnNewButton;

	private Button button_time;
	private Button button_interval;

	private Text dateTime;
	private Text interval;

	public static void main(String[] args) {
		Shell shell2 = new Shell();
		TimeIntervalDialog dialog = new TimeIntervalDialog(shell2, new ConfigFile());
		dialog.open();
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public TimeIntervalDialog(Shell parent, ConfigFile config) {
		super(parent, SWT.CLOSE);
		this.config = config;
	}

	/**
	 * Open the dialog.
	 * @param text 
	 * 
	 * @return the result
	 */
	public void open() {
		createContents();

		/* 屏幕居中打开 */
		Rectangle bounds = Display.getDefault().getPrimaryMonitor().getBounds();
		Rectangle rect = shell.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation(x, y);

		button_time = new Button(shell, SWT.CHECK);
		FormData fd_button_1 = new FormData();
		fd_button_1.top = new FormAttachment(0, 17);
		fd_button_1.left = new FormAttachment(button, 0, SWT.LEFT);
		button_time.setLayoutData(fd_button_1);
		button_time.setText("定时执行：");
		button_time.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (button_time.getSelection()) {
					dateTime.setEnabled(true);
				} else {
					dateTime.setEnabled(false);
					dateTime.setText("00:00:00");
				}
			}
		});

		button_interval = new Button(shell, SWT.CHECK);
		FormData fd_button_2 = new FormData();
		fd_button_2.top = new FormAttachment(button_time, 21);
		fd_button_2.left = new FormAttachment(button, 0, SWT.LEFT);
		button_interval.setLayoutData(fd_button_2);
		button_interval.setText("间隔执行：");
		button_interval.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (button_interval.getSelection()) {
					interval.setEnabled(true);
				} else {
					interval.setEnabled(false);
					interval.setText("0");
				}
			}
		});

		dateTime = new Text(shell, SWT.BORDER);
		dateTime.setText("00:00:00");
		FormData fd_dateTime = new FormData();
		fd_dateTime.left = new FormAttachment(button_time, 6);
		fd_dateTime.top = new FormAttachment(0, 10);
		fd_dateTime.right = new FormAttachment(100, -27);
		dateTime.setLayoutData(fd_dateTime);

		interval = new Text(shell, SWT.BORDER);
		interval.setText("0");
		FormData fd_spinner = new FormData();
		fd_spinner.left = new FormAttachment(button_interval, 6);
		fd_spinner.top = new FormAttachment(dateTime, 18);
		interval.setLayoutData(fd_spinner);

		Label label_2 = new Label(shell, SWT.NONE);
		fd_spinner.right = new FormAttachment(100, -27);
		FormData fd_label_2 = new FormData();
		fd_label_2.left = new FormAttachment(interval, 6);
		fd_label_2.top = new FormAttachment(0, 55);
		label_2.setLayoutData(fd_label_2);
		label_2.setText("秒");
		
		init();
		
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	private void init() {
		dateTime.setEnabled(false);
		interval.setEnabled(false);
		if (config.time != null) {
			dateTime.setText(config.time);
			button_time.setSelection(true);
			dateTime.setEnabled(true);
		}
		if (config.interval != null) {
			interval.setText(config.interval);
			button_interval.setSelection(true);
			interval.setEnabled(true);
		}
	}

	/**
	 * Create contents of the dialog.
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
		shell.setSize(255, 175);
		shell.setText("任务定时设置");
		shell.setLayout(new FormLayout());

		btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (button_time.getSelection()) {
					config.time = dateTime.getText();
				} else {
					config.time = null;
				}
				if (button_interval.getSelection()) {
					config.interval = interval.getText();
				} else {
					config.interval = null;
				}
				shell.dispose();
			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.bottom = new FormAttachment(100, -10);
		fd_btnNewButton.right = new FormAttachment(100, -10);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("  确  定  ");
	}
}
